package com.sjzn.module_park.fragment

import android.annotation.SuppressLint
import android.os.Parcel
import android.os.Parcelable
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.util.Log
import androidx.core.widget.doOnTextChanged
import com.czl.module_park.R
import com.czl.module_park.BR
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.ReverseSearchBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.DayModeUtil
import com.czl.module_park.adapter.ReverseSearchCarListAdapter
import com.czl.module_park.databinding.FragmentReveseSearchCarBinding
import com.gyf.immersionbar.ImmersionBar
import com.sjzn.keyboard.PopupEditTextKeyboard
import com.sjzn.module_park.viewmodel.ReveseSearchCarViewModel

@Route(path = AppConstants.Router.Park.F_REVERSE_CAR_SEARCH)
class ReverseSearchCarFragment() :
    BaseFragment<FragmentReveseSearchCarBinding, ReveseSearchCarViewModel>(), Parcelable {
    private lateinit var mAdapter: ReverseSearchCarListAdapter

    constructor(parcel: Parcel) : this() {

    }

    override fun initContentView(): Int {
        return R.layout.fragment_revese_search_car
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun useBaseLayout(): Boolean {
        return false
    }
    override fun onSupportVisible() {
        ImmersionBar.with(this).fitsSystemWindows(true)
            .statusBarDarkFont(!DayModeUtil.isNightMode(requireContext())).init()
    }

    override fun initData() {
        val p= PopupEditTextKeyboard(context)
        p.attach(binding.etCar,activity)
        mAdapter= ReverseSearchCarListAdapter(this)
        binding.rySearchCar.apply {
            layoutManager= GridLayoutManager(context,2,RecyclerView.VERTICAL,false)
            adapter=mAdapter
        }
        binding.tvSearch.setOnClickListener {
            viewModel.reverseSearchCar()
        }
        binding.etCar.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
//                Log.e("reverse","车牌号："+s.toString())
                viewModel.carNo.set(s.toString())
            }

        })
        binding.ivBack.setOnClickListener {
            viewModel.uC.finishEvent.call()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun initViewObservable() {
//        keyboard.attach(binding.etCar,activity)
//        binding.etCar.inputType=InputType.TYPE_NULL
        viewModel.uc.loadCompleteEvent.observe(this,{
            data-> if (data!=null){
                binding.smartSearchCar.finishRefresh(true)
                mAdapter.data=data.dataItems as MutableList<ReverseSearchBean.DataItem>
                mAdapter.notifyDataSetChanged()
            }
        })
        LiveBusCenter.observePayResultEvent(this, {
            when (it.payType) {
                AppConstants.Constants.PAY_SUCCESS_TYPE-> {
                    back()
                }
            }
        })
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {

    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<ReverseSearchCarFragment> {
        override fun createFromParcel(parcel: Parcel): ReverseSearchCarFragment {
            return ReverseSearchCarFragment(parcel)
        }

        override fun newArray(size: Int): Array<ReverseSearchCarFragment?> {
            return arrayOfNulls(size)
        }
    }
}